﻿using DataAccess;
using KhoaLuan.Utils;
using log4net;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Script.Serialization;
using System.Web.Services;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace KhoaLuan.MainSys.ProcessAJAX
{
    public partial class AjaxResourceType : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }
        [WebMethod]
        public static string GetListResourceType(int currentpage, int numberinpage, string keyword)
        {
            string link = "~/MainSys/Manager/Controls/ctManagementResource.ascx";
            var currentuser = UserLogin.GetUserID;
            if (!SecurityServices.HasPermison(link, currentuser, TypeAudit.View))
                return new JavaScriptSerializer().Serialize(new { Status = false, Cause = "Bạn không có quyền xem danh sách tài nguyên" });
            try
            {
                tbResourceType_Proxy pr = new tbResourceType_Proxy();
                List<DataAccess.tbResourceType> list = new List<DataAccess.tbResourceType>();
                int totalitem = 0;
                if (keyword == "null" | keyword == "")
                {
                    list = pr.SelectAlltbResourceType_Proxy();
                    totalitem = list.Count;
                }
                else
                {
                    list = (pr.SelectNo_tbResourceType_Proxy(keyword)).ToList<DataAccess.tbResourceType>();
                }

                totalitem = list.Count;
                var list2 = list.Skip((currentpage - 1) * numberinpage).Take(numberinpage);
               

                return new JavaScriptSerializer().Serialize(new { Status = "Success", Data = list2, TotalItem = totalitem, CurrentPage = currentpage });
            }
            catch (Exception)
            {
                return new JavaScriptSerializer().Serialize(new { Status = "Error", Data = "" });
            }
        }
        [WebMethod]
        public static string DeleteResourceType(int ResourceTypeID)
        {
            string link = "~/MainSys/Manager/Controls/ctManagementResource.ascx";
            var currentuser = UserLogin.GetUserID;
            if (!SecurityServices.HasPermison(link, currentuser, TypeAudit.Delete))
                return new JavaScriptSerializer().Serialize(new { Status = false, Cause = "Bạn không có quyền xóa tài nguyên" });
            try
            {
                tbResourceType_Proxy pr = new tbResourceType_Proxy();
                if (pr.Delete_tbResourceType_Proxy(ResourceTypeID))
                    return new JavaScriptSerializer().Serialize(new { Status = "Success" });
                return new JavaScriptSerializer().Serialize(new { Status = "Error" });
            }
            catch (Exception)
            {
                return new JavaScriptSerializer().Serialize(new { Status = "Error" });
            }
        }
        [WebMethod]
        public static string Insert_tbResourceType(string ResourceTypeName, int OrgID)
        {
            string link = "~/MainSys/Manager/Controls/ctManagementResource.ascx";
            var currentuser = UserLogin.GetUserID;
            if (!SecurityServices.HasPermison(link, currentuser, TypeAudit.AddNew))
                return new JavaScriptSerializer().Serialize(new { Status = false, Cause = "Bạn không có quyền thêm mới tài nguyên" });
            try
            {
                ILog log = LogManager.GetLogger(typeof(DataAccess.tbResourceType).Name);
                tbResourceType_Proxy pr = new tbResourceType_Proxy();
                tbResourceType a = new tbResourceType();
                a.ResourceTypeID = 0;
                a.ResourceTypeName = ResourceTypeName;
                a.OrgID = OrgID;
                if (pr.Insert_tbResourceType_Proxy(a))
                    log.Info(UserLogin.GetUserName + "Thêm mới tài nguyên  thành công");
                return new JavaScriptSerializer().Serialize(new { Status = "Success" });
            }
            catch (Exception)
            {
                return new JavaScriptSerializer().Serialize(new { Status = "Error" });
            }

        }
        [WebMethod]
        public static string UpdatetbResourceType(int ResourceTypeID, string ResourceTypeName, int OrgID)
        {
            string link = "~/MainSys/Manager/Controls/ctManagementResource.ascx";
            var currentuser = UserLogin.GetUserID;
            if (!SecurityServices.HasPermison(link, currentuser, TypeAudit.Edit))
                return new JavaScriptSerializer().Serialize(new { Status = false, Cause = "Bạn không có quyền chỉnh sửa tài nguyên" });
            ILog log = LogManager.GetLogger(typeof(DataAccess.tbResourceType).Name);
            tbResourceType_Proxy pr = new tbResourceType_Proxy();
            tbResourceType a = new tbResourceType();
            a.ResourceTypeID = ResourceTypeID;
            a.ResourceTypeName = ResourceTypeName;
            a.OrgID = OrgID;
            try
            {

                if (pr.Update_tbResourceType_Proxy(a))
                    return new JavaScriptSerializer().Serialize(new { Status = "Success" });
                return new JavaScriptSerializer().Serialize(new { Status = "Error" });
            }
            catch (Exception)
            {
                return new JavaScriptSerializer().Serialize(new { Status = "Error" });
            }
        }

    }
}